#!/bin/bash

function usage() {
    echo -e "Usage: $(basename $0) -m <module> | -h"
    echo -e "\trepo sync当前目录下所有的work space下的<module>模块"
    echo -e "\t-m <module> 必须，指定需要同步的模块名称"
    echo -e "\t-h 输出此信息"
    exit 1
}

while getopts "m:h" arg
do
    case ${arg} in 
        m)
            module=${OPTARG}
            ;;
        *)
            usage
            ;;
    esac
done

if [ -z "${module}" ]
then
    usage
fi

s=$(find . -name "${module}" -type d)
if [ -z "$s" ]
then
    echo "没有找到${module}"    
    exit 2
fi

# 数组
modDirs=($( find . -name "${module}" -type d |xargs dirname ))
# echo "${#modDirs[@]}"

curDir=$(pwd)
for d in "${modDirs[@]}"
do
    cd "$d"
    echo $d
    repo sync "${module}"
    cd "${curDir}" 
done
